<template>

    <div id="root">
        <div>
            <Top>
                <p>新增地址</p>

            </Top>
            <van-field
                    required v-model="name" center clearable size="10px" placeholder="请填写你的姓名">
            </van-field>
            <van-field
                    required v-model="school" center clearable size="10px" @focus="addsite()" placeholder="小区/写字楼/学校等">
            </van-field>
            <van-field v-model="site" center clearable size="10px" placeholder="填写详细送餐地址">
            </van-field>
            <van-field v-model="ip" center clearable size="10px" placeholder="请填写可以联系到你的手机号码">
            </van-field>
            <van-field v-model="ip2" center clearable size="10px" placeholder="备用联系电话">
            </van-field>
            <van-button type="primary" block @click="add()">新增地址</van-button>
        </div>
        <div>
            <router-view></router-view>
        </div>
    </div>

</template>

<script>
    import Top from "./Top";
    import qs from "../../node_modules/querystring"

    export default {
        name: "Addshipp",
        components: {Top},
        data() {
            return {
                name: "",
                school: "",
                site: "",
                ip: "",
                ip2: "",
                addsite3:""
            }
        },
        created() {
            this.$on("addsite2",(a)=>{
               this.addsite3=a;
               this.school=a.name;
               console.log(a)
            })
        },
        methods: {
            add() {
                let lok = JSON.parse(localStorage.user);
                this.axios.post('https://elm.cangdu.org/v1/users/'+lok.user_id+'/addresses', qs.stringify({
                    user_id: lok.user_id,
                    address: this.school,
                    address_detail: this.site,
                    geohash:this.addsite3.geohash,
                    name: this.name,
                    phone: this.ip,
                    tag: "wu",
                    sex: 1,
                    phone_bk: this.ip2,
                    tag_type: 3,
                })).then(data => {
                    console.log(data.data);
                    if (data.data.success=="添加地址成功"){
                        this.$router.push("/myuser/shipping")
                    }
                })
            },
            addsite() {
              console.log(this.$router);
                this.$router.push("/myuser/shipping/addshipp/addsite");
                // if (this.$store.state.addrouter==2){
                //   this.$router.push("/close/chooseAddress/addshipp/addsite");
                // }else {
                //   this.$router.push("/myuser/shipping/addshipp/addsite");
                // }
            }
        },
      // beforeRouteUpdate(to,from,next){
      //     // console.log(to,from);
      //   console.log(from.path,'新增地址');
      //   this.$store.commit("clickaddrouter",2)
      //   next()
      // }

    }
</script>

<style scoped>
    #root {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        background-color: #f5f5f5;
        /*z-index: 999;*/
    }
</style>